<?php
namespace User\Controller;
use Common\Model\UserModel;
use User\Controller\CommonController;
use Common\Model\InfoContentModel;

class ManagerController extends CommonController {

    public function index(){
        $userInfo = $this->getUserInfo();
        $infoList = $this->getInfoList($userInfo['id'],10);
        $this->assign('userinfo',$userInfo)->assign("infoList",$infoList);
        $this->display("/user/user_index");
    }

    public function user(){
        $key = sprintf('%s_%s',C('COOK_LOGIN_KEY'),'uid');
        $userCookie = cookie($key) ? cookie($key) : '';
        $user = explode('_',$userCookie)[0];
        if(!$user){
            $this->redirect("/login");exit();
        }
        $userModel = new UserModel();
        $userInfo = $userModel->getUserById($user);
        if(IS_POST){
            $result = $userModel->editUserInfo($user);
            if($result){
                $this->success('用户信息修改成功',U('User/manager/user'));
            }else{
                $this->error($userModel->getError());
            }
            exit();
        }
        $this->assign('user',$userInfo);
        $this->display("/user/user_user");
    }

    public function editUserFace(){
        if(IS_POST){
            $key = sprintf('%s_%s',C('COOK_LOGIN_KEY'),'uid');
            $userCookie = cookie($key) ? cookie($key) : '';
            $user = explode('_',$userCookie)[0];
            if($user){
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize = 3145728;// 设置附件上传大小
                $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath = './UploadFiles/'; // 设置附件上传根目录
                $upload->savePath = 'user/face/'; // 设置附件上传（子）目录
                $upload->saveName = date('ymdHis').mt_rand(100,1000);
                $upload->autoSub = false;
                $file = $upload->upload();
                if (!$file) {// 上传错误提示错误信息
                    $this->error = '上传错误,大小限制3Mb';
                    //return false;
                    exit('上传错误,大小限制3Mb');
                } else {
                    $tempPath = './UploadFiles/' . $file['imageFileUpLoad']['savepath'] . $file['imageFileUpLoad']['savename'];
                    $thumSavePath = thumImg($tempPath, 100, 100,'./UploadFiles/user/face/');
                    $data['face'] = $thumSavePath;
                    $data['id'] = $user;
                    M('user')->save($data);
                    $this->redirect(U("User/manager/editUserFace",array('img'=>urlencode($tempPath)),''));
                    exit();
                }
            }else{
                echo '验证失败,请重新登录';
            }
            exit();
        }
        $this->display("/user/user_user_face");
    }

    public function info(){
        $uid = $this->loginUserInfo[0];
        $info = new InfoContentModel();
        $infoListdata = $info->getInfoDataByUid($uid);
        $pageShow = $this->PageShow($infoListdata['count'],'/user-manager-info');

        $this->assign('info',$infoListdata)->assign('page',$pageShow);
        $this->display("/user/user_info");
    }

    public function editPassword(){

        if(IS_POST){
            $userModel = new UserModel();
            $result = $userModel->editPassword();
            if($result){
                $this->success('密码修改成功',U('User/manager/index'));
            }else{
                $this->error($userModel->getError());
            }
            exit();
        }
        $this->display("/user/user_password");
    }

    public function commany(){
        $this->display("/user/user_commany");
    }

    public function website(){
        $this->display("/user/user_website");
    }

    public function getInfoList($uid,$limit){
        $infoModel = new InfoContentModel();
        $infoData = $infoModel->getInfoList($uid,$limit);
        return $infoData;
    }

    public function updateCreateTime($id){
        $infoModel = new InfoContentModel();
        $infoData = $infoModel->updateCreateTime($id);
        if($infoData){
            $this->success('更新成功',U('User/manager/index'));
        }
    }

    public function editeInfo(){
        $id = I('get.id');
        if(IS_POST){
            $infoModel = new InfoContentModel();
            $result = $infoModel->editeInfo($id);
            if($result){
                $this->success('编辑成功',U('User/manager/index'));
            }else{
                $this->error($infoModel->getError());
            }
            exit();
        }
        $info = $this->getInfoById($id);
        $area = C("city_area");
        $this->assign("info",$info)->assign("area",$area);
        $this->display('/user/user_info_edit');
    }

    public function deleteInfo($id){
        $infoModel = new InfoContentModel();
        $infoData = $infoModel->deleteInfo($id);
        if($infoData){
            $this->success('删除成功',U('User/manager/index'));
        }
    }


}